#pragma once
#include <iostream>
#include <string>
#include <stdarg.h>
#include <cstdio>
#include <ctime>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "SmartMutex.hpp"

#ifndef COMMON
#define CSTR(pro) (pro).c_str()
#endif

using namespace std;

enum LogLevel
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};

enum Style
{
    Screen,
    OneFile,
    ClassFile
};

class Log
{
    friend class Conf;
private:
    const Style defaultstyle = Screen;
    const string defaultfilename = "log";
    // void _LogMessage() {}

    // template <class T, class... Arg>
    // void _LogMessage(T &info, Arg... arg)
    // {
    //     cout << info << " ";

    //     _LogMessage(arg...);
    // }

    // template <class... Arg>
    // void _LogMessage(Arg... arg)
    // {
    //     _LogMessage(arg...);
    // }

    std::string LevelToString(int level)
    {
        switch (level)
        {
        case Debug:
            return "Debug";
        case Info:
            return "Info";
        case Warning:
            return "Warning";
        case Error:
            return "Error";
        case Fatal:
            return "Fatal";
        default:
            return "Unknown";
        }
    }

    string TimeToStrLocalTime()
    {
        time_t cur = time(nullptr);

        struct tm *time = localtime(&cur);

        char curtime[128];
        snprintf(curtime, sizeof(curtime), "%d-%d-%d %d:%d:%d",
                 time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
                 time->tm_hour, time->tm_min, time->tm_sec);

        return curtime;
    }

    void WriteToClassFile(const string &level, const string &info)
    {

        WriteToOneFile(level, info);
    }

    void WriteToOneFile(const string &level, const string &info)
    {
        int fd = open(CSTR(_filename + '.' + level), O_CREAT | O_WRONLY | O_APPEND, 0664);
        write(fd, CSTR(info), info.size());
        close(fd);
    }

    void WriteLog(const string &level, const string &info)
    {
        SmartMutex smartmutex(&_mutex);
        switch (_style)
        {
        case Screen:
            cout << info;
            break;
        case OneFile:
            WriteToOneFile("all", info);
            break;
        case ClassFile:
            WriteToClassFile(level, info);
            break;
        default:
            break;
        }
    }

    void LogMessage(LogLevel loglevel, const char *format, va_list &arg)
    {
        char leftbuffer[1024];
        string level = LevelToString(loglevel);
        string curtime = TimeToStrLocalTime();
        string id = to_string(getpid());
        snprintf(leftbuffer, sizeof(leftbuffer), "[日志等级：%s][时间：%s][进程ID：%s]日志信息：", CSTR(level), CSTR(curtime), CSTR(id));

        char rightbuffer[1024];
        vsnprintf(rightbuffer, sizeof(rightbuffer), format, arg);
        va_end(arg);

        string info = leftbuffer;
        info += rightbuffer;

        // cout << info;
        WriteLog(level, info);
    }
    void Enable(Style style)
    {
        _style = style;
    }

public:
    Log() : _style(defaultstyle), _filename(defaultfilename)
    {
        mkdir("log", 0775);
        _filename = "log/" + _filename;
        pthread_mutex_init(&_mutex, nullptr);
    }

    void operator()(LogLevel loglevel, const char *format, ...)
    {
        va_list arg;
        va_start(arg, format);
        LogMessage(loglevel, format, arg);
    }

    // template <class... Args>
    // void LogMessage(int loglevel, Args... arg)
    // {
    //     cout << "[" << LevelToString(loglevel) << "]";
    //     cout << "[" << time(nullptr) << "]";
    //     cout << " : [";
    //     _LogMessage(arg...);
    //     cout << "]" << endl;
    // }

    ~Log() 
    {
        pthread_mutex_destroy(&_mutex);
    }

private:
    Style _style;
    string _filename;
    pthread_mutex_t _mutex;
};

Log log;

class Conf
{
private:
    Conf()
    {
        log.Enable(ClassFile);
    }

    Conf(const Conf &) = delete;
    Conf &operator=(const Conf &) = delete;

    static Conf conf;

public:
    ~Conf()
    {
    }
};

Conf Conf::conf;